package com.kbtd.utils.redis;

import lombok.Data;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

@Data
public class RedisData {
    private LocalDateTime expireTime;
    private Object data;

    public void setExpireTime(Long expireTime) {
        long expire = TimeUnit.MINUTES.toMillis(expireTime) + System.currentTimeMillis();
        Instant instant = Instant.ofEpochSecond(expire);
        //过期时间
        LocalDateTime expireLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        this.expireTime = expireLocalDateTime;
    }

    public boolean isExpire() {
        return expireTime.isBefore(LocalDateTime.now());
    }

    public <T> T getData(Class<T> canvasType) {
        Class<?> dataClass = this.data.getClass();
        if (canvasType.isAssignableFrom(dataClass)) {
            return canvasType.cast(this.getData());
        }
        throw new RuntimeException("Class not from " +
                dataClass.getName() + " to " + canvasType.getName());
    }

    public void flushExpireTime() {

    }
}